1 package edu.jiangxin.apktoolbox.swing.extend.filepanel; 2 3 import edu.jiangxin.apktoolbox.utils.Constants; 4 import edu.jiangxin.apktoolbox.utils.FileUtils; 5 import org.apache.commons.lang3.ArrayUtils; 6 import org.apache.commons.lang3.StringUtils; 7 import org.apache.logging.log4j.LogManager; 8 import org.apache.logging.log4j.Logger; 9 10 import javax.swing.*; 11 import javax.swing.filechooser.FileNameExtensionFilter; 12 import java.awt.*; 13 import java.awt.datatransfer.DataFlavor; 14 import java.awt.datatransfer.Transferable; 15 import java.awt.datatransfer.UnsupportedFlavorException; 16 import java.awt.event.ActionEvent; 17 import java.awt.event.ActionListener; 18 import java.io.File; 19 import java.io.IOException; 20 import java.util.List; 21 22 public class FilePanel extends JPanel { 23 private static final Logger LOGGER = LogManager.getLogger(FilePanel.class.getSimpleName()); 24 private JTextField fileTextField; 25 26 private String buttonText; 27 28 private String currentDirectoryPath = "."; 29 30 private int fileSelectionMode = JFileChooser.FILES_ONLY; 31 32 private String description; 33 34 private String[] extensions; 35 36 private JFileChooser fileChooser; 37 38 private IFileReadyCallback callback; 39 40 public FilePanel(String buttonText) { 41 super(); 42 fileChooser = new JFileChooser(); 43 this.buttonText = buttonText; 44 initUI(); 45 } 46 47 public void setCurrentDirectoryPath(String currentDirectoryPath) { 48 this.currentDirectoryPath = currentDirectoryPath; 49 } 50 51 public void setFileReadyCallback(IFileReadyCallback callback) { 52 this.callback = callback; 53 } 54 55 public void setFileSelectionMode(int fileSelectionMode) { 56 this.fileSelectionMode = fileSelectionMode; 57 } 58 59 public void setDescriptionAndFileExtensions(String description, String[] extensions) { 60 this.description = description; 61 this.extensions = extensions; 62 if (StringUtils.isNotEmpty(description) || (ArrayUtils.isNotEmpty(extensions))) { 63 FileNameExtensionFilter filter = new FileNameExtensionFilter(description, extensions); 64 if (fileChooser != null) { 65 fileChooser.resetChoosableFileFilters(); 66 fileChooser.addChoosableFileFilter(filter); 67 fileChooser.setAcceptAllFileFilterUsed(false); 68 } 69 } else { 70 if (fileChooser != null) { 71 fileChooser.resetChoosableFileFilters(); 72 fileChooser.setAcceptAllFileFilterUsed(true); 73 } 74 } 75 } 76 77 private void initUI() { 78 setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); 79 80 fileTextField = new JTextField(); 81 fileTextField.setPreferredSize(new Dimension(600, 30)); 82 fileTextField.setMaximumSize(new Dimension(1200, 30)); 83 fileTextField.setTransferHandler(new FileTransferHandler()); 84 85 JButton chooseButton = new JButton(buttonText); 86 chooseButton.addActionListener(new OpenDictionaryFileActionListener()); 87 88 add(fileTextField); 89 add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER)); 90 add(chooseButton); 91 } 92 93 public File getFile() { 94 return new File(fileTextField.getText()); 95 } 96 97 class OpenDictionaryFileActionListener implements ActionListener { 98 @Override 99 public void actionPerformed(ActionEvent e) { 100 fileChooser.setCurrentDirectory(new File(currentDirectoryPath)); 101 fileChooser.setFileSelectionMode(fileSelectionMode); 102 fileChooser.setMultiSelectionEnabled(false); 103 if (StringUtils.isNotEmpty(description) || (ArrayUtils.isNotEmpty(extensions))) { 104 FileNameExtensionFilter filter = new FileNameExtensionFilter(description, extensions); 105 fileChooser.addChoosableFileFilter(filter); 106 fileChooser.setAcceptAllFileFilterUsed(false); 107 } else { 108 fileChooser.resetChoosableFileFilters(); 109 fileChooser.setAcceptAllFileFilterUsed(true); 110 } 111 int returnVal = fileChooser.showOpenDialog(FilePanel.this); 112 if (returnVal == JFileChooser.APPROVE_OPTION) { 113 File selectedFile = fileChooser.getSelectedFile(); 114 fileTextField.setText(FileUtils.getCanonicalPathQuiet(selectedFile)); 115 if (callback != null) { 116 callback.onFileReady(selectedFile); 117 } 118 } 119 } 120 } 121 122 class FileTransferHandler extends TransferHandler { 123 public FileTransferHandler() { 124 } 125 126 @Override 127 public boolean importData(JComponent comp, Transferable t) { 128 List<File> files = null; 129 try { 130 files = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor); 131 } catch (IOException e) { 132 LOGGER.error("importData failed: IOException"); 133 } catch (UnsupportedFlavorException e) { 134 LOGGER.error("importData failed: UnsupportedFlavorException"); 135 } 136 if (files == null || files.size() != 1) { 137 LOGGER.error("importData failed: getTransferData failed"); 138 return false; 139 } 140 File file = files.get(0); 141 if ((!file.isFile() && fileSelectionMode == JFileChooser.FILES_ONLY) 142 ||(!file.isDirectory() && fileSelectionMode == JFileChooser.DIRECTORIES_ONLY)) { 143 LOGGER.error("importData failed: fileSelectionMode is not match"); 144 return false; 145 } 146 fileTextField.setText(FileUtils.getCanonicalPathQuiet(file)); 147 if (callback != null) { 148 callback.onFileReady(file); 149 } 150 return true; 151 } 152 153 @Override 154 public boolean canImport(JComponent jComponent, DataFlavor[] dataFlavors) { 155 for (DataFlavor dataFlavor : dataFlavors) { 156 if (DataFlavor.javaFileListFlavor.equals(dataFlavor)) { 157 return true; 158 } 159 } 160 return false; 161 } 162 } 163 }